# -*- coding: utf-8 -*-
import requests
from lxml import etree
webs = [  "alive.github.com",
    "live.github.com",
    "github.githubassets.com",
    "central.github.com",
    "desktop.githubusercontent.com",
    "assets-cdn.github.com",
    "camo.githubusercontent.com",
    "github.map.fastly.net",
    "github.global.ssl.fastly.net",
    "gist.github.com",
    "github.io",
    "github.com",
    "github.blog",
    "api.github.com",
    "raw.githubusercontent.com",
    "user-images.githubusercontent.com",
    "favicons.githubusercontent.com",
    "avatars5.githubusercontent.com",
    "avatars4.githubusercontent.com",
    "avatars3.githubusercontent.com",
    "avatars2.githubusercontent.com",
    "avatars1.githubusercontent.com",
    "avatars0.githubusercontent.com",
    "avatars.githubusercontent.com",
    "codeload.github.com",
    "github-cloud.s3.amazonaws.com",
    "github-com.s3.amazonaws.com",
    "github-production-release-asset-2e65be.s3.amazonaws.com",
    "github-production-user-asset-6210df.s3.amazonaws.com",
    "github-production-repository-file-5c1aeb.s3.amazonaws.com",
    "githubstatus.com",
    "github.community",
    "github.dev",
    "collector.github.com",
    "pipelines.actions.githubusercontent.com",
    "media.githubusercontent.com",
    "cloud.githubusercontent.com",
    "objects.githubusercontent.com",
    'onedrive.live.com',
    'skyapi.onedrive.live.com',
    'outlook.live.com']
iptable =[]
failip =[]
sip=[]
headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "cache-control": "max-age=0",
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62"
}

for web in webs:
    url = "https://ipaddress.com/website/"+web
    ip=[]
    try:
        html = requests.get(url, headers = headers)
        tree = etree.HTML(html.text)
        name_xpath = '//*[@id="dnsinfo"]/*/td[3]/a/text()'
        ip = tree.xpath(name_xpath)
    except:
        pass
    if ip:
        iptable=iptable+[ip[0]]
        sip = sip+[web]
    else:
        failip=failip+[web]
file = './hosts'
with open(file,'w',encoding="utf-8") as f:
    for ip, name in zip(iptable,sip):
        f.write('''%s  %s\n'''%(ip,name))

print("over") 
if failip:
    print(failip)
